/* 
This layer is used for storing service's and user's information,
 * catching the exception,
 * 
*/
using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Data;

using BL.UAClientRemotingInterface;
using BL.BCClientRemotingInterface;
using Common.IO.FileIO;


namespace BL.Client.BudgetCheckBL
{
    public enum OperateResult
    {
        Success = 0,
        Fail = 1,
        DisConnect = 2,
        WrongInput = 3
    }

    public class BudgetCheckServerice
    {
        private string strCurUserId;
        private string strServerIp;
        private string strServerPort;
        private int iRetryTimes;

        private IUserAuthority IUA;
        private IBudgetCheck IBC;

        #region BudgetCheckServerice
        public BudgetCheckServerice(string inifilepath)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            string a = file.IniReadValue("server", "RetryTimes");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = file.IniReadValue("server", "ServerIp");
        }

        public BudgetCheckServerice(string inifilepath, string server_ip)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = server_ip;
        }

        public BudgetCheckServerice(string inifilepath, string server_ip, string server_port)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = server_ip;
            this.strServerPort = server_port;
        }
        #endregion

        #region GetInfo

        public string GetUserId()
        {
            return this.strCurUserId;
        }

        public string GetUserName()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string user_name = this.IUA.GetUserName(this.strCurUserId);
                    return user_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetDepartmentName()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string department_name = this.IUA.GetDepartmentName(this.strCurUserId);
                    return department_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }
        
        //public DataSet GetBudgetType()
        //{

        #endregion

        #region UserIndenrify
        public OperateResult UserIndentify(string server_ip, string user_name, string user_password)
        {
            if (server_ip == "" || user_name == "" || user_password == "")
            {
                return OperateResult.WrongInput;
            }

            this.IUA = (IUserAuthority)Activator.GetObject(typeof(IUserAuthority),
                    "tcp://" + server_ip + ":" +
                    this.strServerPort + "/RemotingCommon");

            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    if (this.IUA.FDUserIndentify(user_name, user_password))
                    {
                        this.IBC = (IBudgetCheck)Activator.GetObject(typeof(IBudgetCheck),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.strCurUserId = this.IUA.GetUserId(user_name);
                        this.strServerIp = server_ip;

                        string user_hostname = Dns.GetHostName();
                        IPAddress[] iplist = Dns.GetHostAddresses(user_hostname);
                        string user_ip = iplist[iplist.Length - 1].ToString();
                        this.IUA.RaiseUserLogin(user_name, user_ip, user_hostname);

                        return OperateResult.Success;
                    }
                    else
                        return OperateResult.Fail;
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    i++;
                    continue;
                }
            }
            return OperateResult.DisConnect;
        }

        public void UserLogOut()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string user_hostname = Dns.GetHostName();
                    this.IUA.RaiseUserLogout(user_hostname);
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    i++;
                    continue;
                }
            }
            
        }
        #endregion

        //#region BudgetCheck
        //public OperateResult BudgetCheck(System.Data.DataTable budgetdata)
        //{
        //    int i = 0;
        //    while (i < this.iRetryTimes)
        //    {
        //        try
        //        {
        //            if (this.IBR.BudgetReport(budgetdata, this.strCurUserId))
        //                return OperateResult.Success;
        //            else
        //                return OperateResult.Fail;
        //        }
        //        catch (Exception ex)
        //        {
        //            i++;
        //            continue;
        //        }
        //    }
        //    return OperateResult.DisConnect;
        //}

        //public OperateResult BudgetReport(string budget_name, string budget_price,
        //                        string budget_type_id, string budget_remark)
        //{
        //    int i = 0;
        //    while (i < this.iRetryTimes)
        //    {
        //        try
        //        {
        //            if (this.IBR.BudgetReport(budget_name, budget_price,
        //                        budget_type_id, budget_remark, this.strCurUserId))
        //                return OperateResult.Success;
        //            else
        //                return OperateResult.Fail;
        //        }
        //        catch (Exception ex)
        //        {
        //            i++;
        //            continue;
        //        }
        //    }
        //    return OperateResult.DisConnect;
        //}

        //public DataSet GetBudgetType()
        //{
        //    int i = 0;
        //    while (i < this.iRetryTimes)
        //    {
        //        try
        //        {
        //            return this.IBR.GetBudgetType();
        //        }
        //        catch (Exception ex)
        //        {
        //            i++;
        //            continue;
        //        }
        //    }
        //    return null;
        //}

        //public DataSet GetBudgetSubType(string budget_type_id)
        //{
        //    int i = 0;
        //    while (i < this.iRetryTimes)
        //    {
        //        try
        //        {
        //            return this.IBR.GetBudgetSubType(budget_type_id);
        //        }
        //        catch (Exception ex)
        //        {
        //            i++;
        //            continue;
        //        }
        //    }
        //    return null;
        //}

        //#endregion



    }
}
